import { api_getQuestionList } from '@/api'
import { useRequest } from 'ahooks'
import { useSearchParams } from 'react-router-dom'
import { searchOptionsType } from '@/types'

const useQuestionDataList = (params: Partial<searchOptionsType> = {}) => {
  const [searchParams] = useSearchParams()
  const keyword = searchParams.get('keyword')
  const page = Number(searchParams.get('page')) || 1
  const pageSize = Number(searchParams.get('pageSize')) || 5
  params.page = page
  params.pageSize = pageSize
  if (keyword) {
    params.keyword = keyword
  }

  const {
    loading,
    data = {},
    refresh,
  } = useRequest(
    async () => {
      return await api_getQuestionList({ ...params })
    },
    {
      refreshDeps: [keyword, params.page, params.pageSize],
    }
  )
  const { list = [], total = 0 } = data

  return {
    loading,
    list,
    total,
    refresh,
  }
}

export default useQuestionDataList
